<?php

class Country_Plugin_SetParam extends Zend_Controller_Plugin_Abstract
{
    
  public function onUserCreateAfter($payload)
  {
    $user = $payload->getPayload();
    $session = new Zend_Session_Namespace('invite');
    $inviteTable = Engine_Api::_()->getDbtable('invites', 'invite');
    $isEligible = Engine_Api::_()->getApi('settings', 'core')->user_friends_eligible;
    //$inviteTable = new Zend_Db_Table();

    // Get codes
    $codes = array();
    if( !empty($session->invite_code) ) {
      $codes[] = $session->invite_code;
    }
    if( !empty($session->signup_code) ) {
      $codes[] = $session->signup_code;
    }
    $codes = array_unique($codes);

    // Get emails
    $emails = array();
    if( !empty($session->invite_email) ) {
      $emails[] = $session->invite_email;
    }
    if( !empty($session->signup_email) ) {
      $emails[] = $session->signup_email;
    }
    $emails = array_unique($emails);

    // Nothing, exit now
    if( empty($codes) && empty($emails) ) {
      return;
    }
    
    // Get related invites
    $select = $inviteTable->select();

    if( !empty($codes) ) {
      $select->orWhere('code IN(?)', $codes);
    }

    if( !empty($emails) ) {
      $select->orWhere('recipient IN(?)', $emails);
    }
    
    $countryMemberT = Engine_Api::_()->getDbtable('countrymembers','country');
    foreach( $inviteTable->fetchAll($select) as $invite ) {
      $countryMemberT->joinMember($user->user_id,$invite->country_id);
    }

     // Clean session
    $session->unsetAll();
  }
  
  
  
  
  
  
  public function  preDispatch(Zend_Controller_Request_Abstract $request) 
  {
     $controller = $request->getControllerName();
     $action = $request->getActionName();
     $module = $request->getModuleName();
     if ($module == 'country' && $controller == 'index' && $action == 'view'){
         $paramString = $request->getParam('pr','');
         if (empty($paramString)) {
            $paramString = 'products';
         }
         if (!empty($paramString)  ){
            $params = explode('-',$paramString);
            if (count($params) > 0){
                $table = Engine_Api::_()->getDbtable('listingtypes','sitereview');
                $select = $table->select()
                                ->where('slug_plural = ?',$params[0])
                                ->orWhere('slug_singular = ?',$params[0]);
                $listingType = $table->fetchRow($select);
                $request->setParam('listingtype_id',$listingType->listingtype_id);
                $request->setParam('title_plural',$listingType->title_plural) ;
                $request->setParam('type', $listingType->template_type);
                $request->setParam('listingObject',$listingType);
                
                $paramSupport = array(
                    'category_id',
                    'categoryname',
                    'subcategory_id' , 
                    'subcategoryname',
                    'subsubcategory_id',
                    'subsubcategoryname'                  
                );
                $request->setParam('hasCategory',0);
                $request->setParam('profile',0);
                if (count($params) > 1){
                    
                    if ($params[1] == "manage"){
                        $request->setParam('profile',1);
                    }
                    
                    if ($params[1] == 'index'){
                        $request->setParam('hasCategory',1);
                        
                    } else if ($params[1] != "manage") {
                        for( $i = 1; $i < count($params); $i++){
                            $request->setParam($paramSupport[$i-1],$params[$i]);
                        }
                        $request->setParam('hasCategory',1);
                    }
                    
                    
                    
                }
                
            }
            
            
            
         }
         
         
  
     }
  }
}